package com.sihan.framework.etm.common;

import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import com.framework.model.ApiResult;

@Component
public class RestBiz {
	
	@Autowired
	private RestTemplate restTemplate;

	public ResponseEntity<?> postForEntity(String restApiUrl, Object request, 
			Class<?> responseType,Map<String,String> header) throws Exception {
		try {
			HttpHeaders headers = new HttpHeaders();
			if(null != header && !header.isEmpty()) {
				for(Map.Entry<String,String> entry : header.entrySet()) {
					headers.set(entry.getKey(),entry.getValue());
				}
			}
			HttpEntity<Object> entityrequest = new HttpEntity<Object>(request, headers);
			ResponseEntity<?> entity = restTemplate.postForEntity(restApiUrl, entityrequest, responseType);
			return entity;
		} catch (HttpClientErrorException ex) {
			if (ex.getStatusCode() == HttpStatus.UNPROCESSABLE_ENTITY) {
				String str = ex.getResponseBodyAsString();
				return new ResponseEntity<String>(str, UNPROCESSABLE_ENTITY);
			} else {
				throw ex;
			}
		}
	}
	
	public ResponseEntity<?> getForEntity(String url, Class<?> responseType,Map<String,String> header, 
			Map<String,Object> urlVariables) throws Exception {
		try {
			HttpHeaders headers = new HttpHeaders();
			if(null != header && !header.isEmpty()) {
				for(Map.Entry<String,String> entry : header.entrySet()) {
					headers.set(entry.getKey(),entry.getValue());
				}
			}
			
			HttpEntity<?> requestEntity = new HttpEntity<>(headers);
			ResponseEntity<?> entity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, responseType,
					urlVariables);
			return entity;
		} catch (HttpClientErrorException ex) {
			if (ex.getStatusCode() == HttpStatus.UNPROCESSABLE_ENTITY) {
				String str = ex.getResponseBodyAsString();
				return new ResponseEntity<String>(str, HttpStatus.NOT_FOUND);
			} else if (ex.getStatusCode() == HttpStatus.NOT_FOUND) {
				return new ResponseEntity<String>("", HttpStatus.NOT_FOUND);
			} else {
				throw new Exception(ex);
			}
		}
	}
	
	public ResponseEntity<?> postForEntity(String url,Object request,Class<?> responseType) {
		return restTemplate.postForEntity(url,request,responseType);
	}
	
	public ResponseEntity<?> postForEntityOhter(String url,Object request,Class<?> responseType,Map<String, ?> uriVariables) {
		return restTemplate.postForEntity(url,request,responseType,uriVariables);
	}
	
	public ResponseEntity<?> getForEntity(String url,Class<?> responseType) {
		return restTemplate.getForEntity(url,responseType);
	}
	
	public ResponseEntity<?> getForEntity(String url,Map<String,?> params,Class<?> responseType) {
		return restTemplate.getForEntity(url, responseType, params);
	}
	
	public ResponseEntity<?> getForEntityUseExchange(String url,Object param,ParameterizedTypeReference<ApiResult<List<?>>> reference) {
		return restTemplate.exchange(url, HttpMethod.GET,new HttpEntity<Object>(param), reference);
	}
	
	public ResponseEntity<?> getForEntityUseExchangeGeneric(String url,Object param,ParameterizedTypeReference<?> reference) {
		return restTemplate.exchange(url, HttpMethod.GET,new HttpEntity<Object>(param), reference);
	}
	
}
